package com.emergency.evaluate.service.impl;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.evaluate.dao.*;
import com.emergency.evaluate.domain.*;
import com.emergency.evaluate.service.AutoService;
import com.emergency.evaluate.service.ManualService;
import org.apache.commons.math.linear.MatrixUtils;
import org.apache.commons.math.linear.RealMatrix;
import org.hibernate.boot.jaxb.hbm.spi.Adapter1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2020/3/16.
 */
@Service
@Transactional
public class AutoServiceImpl implements AutoService {
    //输出日志
    private final static Logger logger = LoggerFactory.getLogger ( AutoServiceImpl.class );
    @Autowired
    private AutoDao autoDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private AutoscoreDao autoscoreDao;
    @Autowired
    private ExtractDao extractDao;
    @Autowired
    private ReportDao reportDao;


    //根据想定编号查询评分表
    @Override
    public List <Auto> findAllByScenar(Long scenarId) {
        List <Auto> autos = autoDao.findAllByScenar ( scenarId );
        logger.info ( "根据想定编号查询评分表" );
        return autos;
    }

    //保存评分表
    @Override
    public Auto save(Auto auto) {
        return autoDao.save ( auto );
    }

    //删除评分表
    @Override

    public void delete(Long id) {
        //先删除autoscore表里的数据
        autoscoreDao.deleteAllByAutoId ( id );
        //在把manual删除
        autoDao.deleteById ( id );
        //把manual表对应的report也删除
        reportDao.deleteByAuto ( id );
    }

    //添加人工评分表
    @Override

    public List <Auto> add(Auto auto) {

        //manual没有ID则自动就增加ID
        if (CommonUtils.isNullOrEmpty ( auto.getId () )) {
            auto.setId ( idWorker.nextId () );
            auto.setCreateTime ( new Date () );
        }

        // 如果没有选择打分方式
        if (auto.getMethod () == null) {
            auto.setMethod ( 2 );//默认打分方式为人工评估
        }
        autoDao.save ( auto );
        //先删除manual 相关的分数设置
        autoscoreDao.deleteByAuto ( auto.getId () );
        double ascore = 0;
        //系统评分，对应的自动评分表录入数据起始信息
        List <Autoscore> autoscores = auto.getAutoscores ();
        for (Autoscore autoscore : autoscores) {
            Autoscore autoscore1 = autoscoreDao.findAllById ( autoscore.getId () );
            //计算规则：在误差5%内为优秀，10%内为良好，15%内为中等，30%及格
            //导入数据集
            double i=autoscore1.getDataStandard ();
            double x[] = new double[]{i,0.95*i,0.9*i,0.85*i,0.7*i,1.05*i,1.1*i,1.15*i,1.3*i};
            double y[] = new double[]{100,90,80,70,60,90,80,70,60};

            //最小二乘法曲线拟合
            LeastSquareMethod eastSquareMethod = new LeastSquareMethod( x, y, 6);
		    autoscore1.setScore ( eastSquareMethod.fit(autoscore1.getDataEnd ()));
            //设置分数等级
            if (autoscore1.getScore () < 100 && (autoscore1.getScore () >= 90)) {
                autoscore1.setLevel ( "A" );
            } else if (autoscore1.getScore () < 90 && (autoscore1.getScore () >= 80)) {
                autoscore1.setLevel ( "B" );
            } else if (autoscore1.getScore () < 80 && (autoscore1.getScore () >= 70)) {
                autoscore1.setLevel ( "C" );
            } else if (autoscore1.getScore () < 70 && (autoscore1.getScore () >= 60)) {
                autoscore1.setLevel ( "D" );
            } else if (autoscore1.getScore () < 60) {
                autoscore1.setLevel ( "E" );
            }
            autoscore1.setAuto ( auto );
            ascore = ascore + autoscore1.getScore () * autoscore1.getExtract ().getWeight (); //总分等于分数*权重
            autoscoreDao.save ( autoscore1 );
        }
        auto.setAutosum (ascore);
        autoDao.save (auto);
        return autoDao.findallById ( auto.getId () );
    }

    @Override
    public Auto get(Long id) {
        Auto auto = autoDao.findAllById ( id );
        if (CommonUtils.isNullOrEmpty ( auto )) {
            logger.info ( "评分表不存在！" );
            return auto;
        }
        auto.setAutoscores ( autoscoreDao.findAllByAuto ( id ) );
        return auto;
    }
}